//
//  OuptPrescListViewController.m
//  掌上宏力
//
//  Created by admin on 2017/5/15.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "OuptPrescListViewController.h"
#import "OuptPrescListModel.h"
#import "OuptPrescListCell.h"
#import "OutPrescListViewModel.h"

@interface OuptPrescListViewController ()

@property (nonatomic,strong) OutPrescListViewModel *viewModel;
@property (nonatomic,strong) NSMutableArray *datasArray;

@end

@implementation OuptPrescListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"处方记录";
}

-(void)getDatasArray{
    [self getDatasArray:^(NSString *page, GetDatas handler) {
        _viewModel = [[OutPrescListViewModel alloc] init];
        NSString *p = [NSString stringWithFormat:@"%ld",self.currentPage];
        NSDictionary *dic = @{@"patient_id":[User currentUser].patientId,@"Page":p};
        [_viewModel getPrescListWithDic:dic success:^(NSArray *array) {
            if (self.currentPage == 1) {
                _datasArray = [NSMutableArray arrayWithArray:_viewModel.datasArray];
            }else{
                [_datasArray addObjectsFromArray:_viewModel.datasArray];
            }
            handler(_viewModel.datasArray);

        }];
       
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _datasArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    OuptPrescListModel *model = _datasArray[section];
    return model.visitDate;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OuptPrescListCell *cell = [OuptPrescListCell cellWithTableView:tableView];
    cell.prescList = _datasArray[indexPath.section];
    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == _datasArray.count - 1) {
        self.currentPage ++;
        [self getDatasArray];
    }
}

@end
